package builder.LogFormBuilderPackage;


import handler.logFormHandlers.LFComboBoxAssetSelectorListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import builder.FormBuilder;
import builder.FormDirector;
import builder.AssetFormBuilderPackage.ViewAssetFormBuilder;
import db.Asset;

import form.AssetForm.AssetForm;
import form.LogForm.LogForm;

public class LogFormBuilder extends FormBuilder {
	
	public LogFormBuilder(){
		super(new LogForm());
	}
	
	@Override
	public void setFormSettings() {
		form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));
	}

	@Override
	public void buildComponents() {
		LogForm logForm = (LogForm) form;
		
		//Build the table and its scrollpane
		JTable table = buildTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(new LFTableMouseListener(logForm));
		logForm.setTable(table);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(table);
		
		//Build the selector
		JComboBox<String> selector = new JComboBox<String>();
		selector.addActionListener(new LFComboBoxAssetSelectorListener(table, selector));
		logForm.setSelector(selector);
		
		//Build the ViewForm
		FormBuilder viewFormBuilder = new ViewAssetFormBuilder();
		FormDirector viewFormDirector = new FormDirector(viewFormBuilder);
		viewFormDirector.construct();
		logForm.setViewForm((AssetForm)viewFormDirector.getForm());
		
		//Add the components to the logForm
		logForm.add(selector);
		logForm.add(scrollPane);
		logForm.add(viewFormDirector.getForm());
		logForm.refresh();
	}

	
	private JTable buildTable() {
		JTable table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setEnabled(false);
		table.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null},
				},
				new String[] {
					"DateTime", "Type of Action", "Asset ID", "Asset name", "Username"
				}
			));
		table.getColumnModel().getColumn(1).setPreferredWidth(92);
		table.getColumnModel().getColumn(2).setPreferredWidth(80);
		table.setRowSelectionAllowed(true);
	
		return table;
	}
}


class LFTableMouseListener extends MouseAdapter{
	
		private LogForm logForm;
	
		public LFTableMouseListener(LogForm logForm){
			this.logForm = logForm;
		}
	
		@Override
		public void mouseClicked(MouseEvent event) {
			JTable table = logForm.getTable();
			
			 int row = table.rowAtPoint(event.getPoint());
			 int col = table.columnAtPoint(event.getPoint());
			 
			 String tableName = table.getModel().getValueAt(row, 4).toString();
			 String assetName = table.getModel().getValueAt(row, 2).toString();
			 String assetID = table.getModel().getValueAt(row, 1).toString();
			 String dateTime = table.getModel().getValueAt(row, 0).toString();

			 
			 if (tableName.compareTo("Add")== 0){
				 tableName = "addlog";
			 	 setValues(assetID, tableName, dateTime);}
			 else if (tableName.compareTo("Edit")==0){
				 tableName = "editlog";
				 setValues(assetID, tableName, dateTime);}
			 else if (tableName.compareTo("Delete")==0){
				 tableName = "deletelog";
				 setValues(assetID, tableName, dateTime);}
		}
		
		private void setValues(String assetID, String tableName, String dateTime){	
			Asset selectedAsset = Asset.createAssetFromIDTableDateTime(Integer.parseInt(assetID),tableName,dateTime);
			logForm.getViewForm().populate(selectedAsset); 
		}
}

